/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template dir, choose Tools | Templates
 * and open the template in the editor.
 */
package org.easyjava.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.easyjava.util.JacksonNode;
import org.easyjava.util.XMLUtil;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

/**
 *
 * @author 朱雀
 */
public class Explorer extends ServerResource {

	public static String explorerRootPath;
	private static String packageLogo = "#package";

	static {
		if (explorerRootPath == null) {
			PropertyResourceBundle prb = (PropertyResourceBundle) ResourceBundle.getBundle("Path");
			explorerRootPath = prb.getString("explorerRootPath");
		}
	}

	@Get
	public ObjectNode getFiles() {
		String dirName = getQuery().getValues("dirName");
		boolean isPackage = false;
		if (dirName.endsWith(packageLogo)) {
			dirName = dirName.replace(packageLogo, "");
			isPackage = true;
		}
		ObjectNode selectNode = JacksonNode.getMapper().createObjectNode();
		ArrayNode explorerArray = JacksonNode.getMapper().createArrayNode();
		selectNode.put("explorer", explorerArray);
//		dirName = (dirName == null || dirName.equals("")) ? explorerRootPath : dirName;

		String IDE = null;
		if (!isPackage && dirName.endsWith("src")) {
			String parent = dirName.substring(0, dirName.length() - 4);
			File parentDir = new File(explorerRootPath, parent);
			String[] brotherlist = parentDir.list();
			for (String brother : brotherlist) {
				if (brother.equals(".project")) {
					String xmlString = readFile(new File(parentDir, brother));
					ObjectNode xml = XMLUtil.encode(xmlString);
					if (xml.findValues("projectDescription") != null) {
						IDE = "eclipse";
					}
				} else if (IDE == null && brother.equals("build.xml")) {
					String xmlString = readFile(new File(parentDir, brother));
					ObjectNode xml = XMLUtil.encode(xmlString);
					if (xml.findValues("project") != null) {
						IDE = "netbeans";
					}
				}
			}
			String srcPath = null;
			if (IDE != null && IDE.equals("eclipse")) {
				srcPath = dirName;
			} else if (IDE != null && IDE.equals("netbeans")) {
				srcPath = dirName + File.separator + "java";
			}
			if (srcPath != null) {
				File src = new File(explorerRootPath, srcPath);
				loadPackage(srcPath, explorerArray, src);
			}
		}
		if (IDE == null) {
			File dir = new File(explorerRootPath, dirName);
			File[] filelist = dir.listFiles();
			for (File thisFile : filelist) {
				if (thisFile.isDirectory() && isPackage) {
					continue;
				}
				ObjectNode thisNode = new ObjectMapper().createObjectNode();
				String thisPath = thisFile.getPath().replace(explorerRootPath, "");
				if (thisPath.startsWith(File.separator)) {
					thisPath = thisPath.substring(1);
				}
				thisNode.put("id", thisPath);
				thisNode.put("fileName", thisFile.getName());
				thisNode.put("text", thisFile.getName());
				if (thisFile.isDirectory()) {
					thisNode.put("leaf", false);
				} else {
					thisNode.put("leaf", true);
				}
				explorerArray.add(thisNode);
			}
		}
		return selectNode;
	}

	@Post
	public String readFile(File file) {
		String text = null;
		try {
			if (file == null) {
				String path = getQuery().getValues("path");
				file = new File(explorerRootPath, path);
			}
			RandomAccessFile randomFile = null;
			randomFile = new RandomAccessFile(file, "r");
			long fileLength = randomFile.length();
			randomFile.seek(0);
			byte[] bytes = new byte[(int) fileLength];
			int byteread = 0;
			while ((byteread = randomFile.read(bytes)) != -1) {
//				System.out.write(bytes, 0, byteread);
			}
			text = new String(bytes, "UTF-8");
			randomFile.close();
		} catch (FileNotFoundException ex) {
			Logger.getLogger(Explorer.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(Explorer.class.getName()).log(Level.SEVERE, null, ex);
		}
		return text;
	}

	private void loadPackage(String srcPath, ArrayNode explorerArray, File dir) {
		File[] filelist = dir.listFiles();
		ArrayList<File> dirList = new ArrayList<File>();
		boolean isPackage = false;
		for (File file : filelist) {
			if (file.isDirectory()) {
				dirList.add(file);
			} else {
				isPackage = true;
			}
		}
		if (isPackage) {
			ObjectNode packageNode = new ObjectMapper().createObjectNode();
			String thisPath = dir.getPath().replace(explorerRootPath, "");
			if (thisPath.startsWith(File.separator)) {
				thisPath = thisPath.substring(1);
			}
			String packageName = thisPath.replace(srcPath, "").replace(File.separator, ".");
			if (packageName.startsWith(".")) {
				packageName = packageName.substring(1);
			} else if (packageName.equals("")) {
				packageName = "默认包";
				thisPath = thisPath + packageLogo;
			}
			packageNode.put("id", thisPath);
			packageNode.put("fileName", packageName);
			packageNode.put("text", packageName);
			packageNode.put("leaf", false);
			explorerArray.add(packageNode);
		}
		for (File file : dirList) {
			loadPackage(srcPath, explorerArray, file);
		}
	}

}
